package io.renren.modules.demo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.entity.XlsCirclePriEntity;
import io.renren.common.enums.CircleLevelEnum;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.demo.dao.XlsCirclePriDao;
import io.renren.modules.demo.dto.MyEcharsDTO;
import io.renren.modules.demo.dto.XlsCirclePriDTO;
import io.renren.modules.demo.dto.XlsUserCircleDTO;
import io.renren.modules.demo.service.XlsCirclePriService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 新零售初级圈子-七人一组
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-04-13
 */
@Service
public class XlsCirclePriServiceImpl extends CrudServiceImpl<XlsCirclePriDao, XlsCirclePriEntity, XlsCirclePriDTO> implements XlsCirclePriService {

    @Override
    public QueryWrapper<XlsCirclePriEntity> getWrapper(Map<String, Object> params) {

        QueryWrapper<XlsCirclePriEntity> wrapper = new QueryWrapper<>();

        return wrapper;
    }

    @Override
    public PageData<XlsCirclePriDTO> queryPage(Map<String, Object> params) {
        QueryWrapper<XlsCirclePriEntity> wrapper = getWrapper(params);
        wrapper.le("xu.circle_level", CircleLevelEnum.PARTNER_LEVEL.value());
        wrapper.ge("xu.circle_level", CircleLevelEnum.PRIMARY_LEVEL.value());
        if(ObjectUtil.isNotEmpty(params.get("mobile"))) {
            wrapper.eq("xu.mobile", params.get("mobile"));
        }
        IPage<XlsCirclePriDTO> page = baseDao.queryPage(
                getPage(params, null, false),
                wrapper
        );

        return getPageData(page, currentDtoClass());
    }

    @Override
    public MyEcharsDTO getRootOneSecondThree(String topUserId) {
        //获取一级
        List<XlsUserCircleDTO> one = this.baseDao.getOne(topUserId);
        //设置一级
        MyEcharsDTO myEcharsDTO = MyEcharsDTO.builder()
                .name(one.get(0).getUsername()+getCircleLevel(one.get(0).getCircleLevel(),one.get(0).getMobile()))
                .xlsUserId(one.get(0).getXlsUserId())
                .build();
        //获取二级
        List<XlsUserCircleDTO> two = this.baseDao.getTwo(topUserId);
        //创建二级echarts实体List
        List<MyEcharsDTO> secondList = new ArrayList<>();
        //设置二级
        two.forEach(second -> {
            secondList.add(MyEcharsDTO.builder()
                    .name(second.getUsername() + getCircleLevel(second.getCircleLevel(),second.getMobile()))
                    .xlsUserId(second.getXlsUserId())
                    .children(new ArrayList<>())
                    .build());
        });

        //加入三级
        secondList.forEach(second -> {
            //创建三级echarts实体List
            List<MyEcharsDTO> threeList = new ArrayList<>();
            //获取三级
            List<XlsUserCircleDTO> three = this.baseDao.getThree(second.getXlsUserId());
            three.forEach(threeDto -> {
                threeList.add(MyEcharsDTO.builder()
                        .name(threeDto.getUsername()+getCircleLevel(threeDto.getCircleLevel(),threeDto.getMobile()))
                        .xlsUserId(threeDto.getXlsUserId())
                        .children(new ArrayList<>())
                        .build());
            });
            second.setChildren(threeList);
        });
        myEcharsDTO.setChildren(secondList);

        return myEcharsDTO;
    }

    @Override
    public List<MyEcharsDTO> getXlsUserSecondThree(String topUserId) {
        //获取二级
        List<XlsUserCircleDTO> two = this.baseDao.getTwo(topUserId);
        //创建二级echarts实体List
        List<MyEcharsDTO> secondList = new ArrayList<>();
        //设置二级
        two.forEach(second -> {
            secondList.add(MyEcharsDTO.builder()
                    .name(second.getUsername() + getCircleLevel(second.getCircleLevel(),second.getMobile()))
                    .xlsUserId(second.getXlsUserId())
                    .children(new ArrayList<>())
                    .build());
        });

        //加入三级
        secondList.forEach(second -> {
            //创建三级echarts实体List
            List<MyEcharsDTO> threeList = new ArrayList<>();
            //获取三级
            List<XlsUserCircleDTO> three = this.baseDao.getThree(second.getXlsUserId());
            three.forEach(threeDto -> {
                threeList.add(MyEcharsDTO.builder()
                        .name(threeDto.getUsername()+getCircleLevel(threeDto.getCircleLevel(),threeDto.getMobile()))
                        .xlsUserId(threeDto.getXlsUserId())
                        .children(new ArrayList<>())
                        .build());
            });
            second.setChildren(threeList);
        });


//        List<MyEcharsDTO> threeList = new ArrayList<>();
//        //获取三级
//        List<XlsUserCircleDTO> three = this.baseDao.getThree(topUserId);
//        three.forEach(threeDto -> {
//            threeList.add(MyEcharsDTO.builder()
//                    .name(threeDto.getUsername()+getCircleLevel(threeDto.getCircleLevel(),threeDto.getMobile()))
//                    .xlsUserId(threeDto.getXlsUserId())
//                    .children(new ArrayList<>()).build());
//        });
        return secondList;
    }

    /**
     * 获取待升级的等级
     *
     * @param circleLevel
     * @return
     */
    private String getCircleLevel(String circleLevel,String mobile) {
        String result = "";
        switch (circleLevel) {
            case "0":
                result = "【初级 "+mobile+"】";
                break;
            case "1":
                result = "【县级 "+mobile+"】";
                break;
            case "2":
                result = "【市级 "+mobile+"】";
                break;
            case "3":
                result = "【省级 "+mobile+"】";
                break;
            case "4":
                result = "【合伙人 "+mobile+"】";
                break;
            case "5":
                result = "【普通用户 "+mobile+"】";
                break;
        }

        return result;
    }

}